﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Logic;
using Logic.Models;
using Logic.ClarizenSvc;
using Logic.ClarizenObjects;

namespace ClarizenGadget.Controllers
{
    public class ProjectsController : Controller
    {
        [HttpPost]
        public ActionResult Index()
        {
            var projects = new QueryComponent().GetListByTypeName("Project", "", null, new string[] { "Name" }, false).Select(p=> new Project(p));
            return Json(projects);
        }

        //public ActionResult ProjectTable()
        //{
        //    var projects = new QueryComponent().GetWorkItems(WorkItemType.Project, Logic.ClarizenSvc.WorkItemFilter.All, WorkItemState.All, "", null);
        //    //var projects = new QueryComponent().GetListByTypeName("Project", "", null, BaseObject.GetFields(typeof(Project))).Select(p => new Project(p));
        //    return View(projects);
        //}

        [HttpPost]
        public ActionResult ProjectTableData()
        {
            var projects = new QueryComponent().GetWorkItems(WorkItemType.Project, Logic.ClarizenSvc.WorkItemFilter.All, WorkItemState.All, "", null);
            return Json(new GadgetResponse<Object>
            {
                Status = GadgetResponseStatus.Success,
                Data = new
                    {
                        projects = projects,
                        roles = Enum.GetNames(typeof(Logic.ClarizenSvc.WorkItemFilter)).Select(f => new { Key = f.Spacify(), Value = f }),
                        states = Enum.GetNames(typeof(Logic.ClarizenSvc.WorkItemState)).Select(f => new { Key = f.Spacify(), Value = f }),
                    }
            });
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult WorkItemChildren(string parentID, string parentType, string filter, Logic.ClarizenSvc.WorkItemType type = WorkItemType.All, Logic.ClarizenSvc.WorkItemFilter role = WorkItemFilter.All, WorkItemState state = WorkItemState.All)
        {
            var children = new QueryComponent().GetWorkItems(type, role, state, filter, parentID, parentType);
            return Json(new GadgetResponse<Object> { Status = GadgetResponseStatus.Success, Data = children });
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult AttachEmail(WorkItem workItem, Email email)
        {
            var emailEntity = email.ToGenericEntity();
            var values = emailEntity.Values.ToList();
            values.Add(new FieldValue { FieldName = "AttachedTo", Value = workItem.ToGenericEntity() });
            emailEntity.Values = values.ToArray();

            new QueryComponent().CreateEntity(emailEntity);

            return Json(true);
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Create(Project p)
        {
            try
            {
                var project = p.ToGenericEntity();
                var res = new QueryComponent().Execute(new BaseMessage[]{new CreateMessage{Entity=project}});

                p.ID = new EntityId { TypeName = "Project", Value = ((Logic.ClarizenSvc.CreateResult)(res[0])).Id.Value };
                return Json(new GadgetResponse<Project> { Data = p, Status = GadgetResponseStatus.Success });
            }
            catch (Exception e)
            {
                return Json(new GadgetResponse<Task> { Status = GadgetResponseStatus.Error, ErrorMessage = e.Message });
            }
        }
    }
}
